package com.justcan.demo.cartoon.utils;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;

import com.justcan.demo.cartoon.utils.anim.touch.TouchAnimObjectGroup;


public class RocketAnimGroup extends TouchAnimObjectGroup {
	/**
	 * 火箭
	 */
	private RocketObject mRocketObject = null;
	/**
	 * 发射台
	 */
	private LaunchPadObject mLaunchPadObject = null;
	/**
	 * 雾霾
	 */
	private FogObject mFogObject = null;
	/**
	 * 小雾霾
	 */
	private SmallFogObject mSmallFogObject = null;
	/**
	 * 动画阶段
	 * 第一阶段，准备发射
	 * 第二阶段，起飞
	 */
	public static final int ANIM_STAGE_READY = 1;
	public static final int ANIM_STAGE_LAUNCH = 2;
	private int mAnimStage = ANIM_STAGE_READY;
	/**
	 * 是否可点击
	 */
	private boolean mCanTouch = true;

	public RocketAnimGroup(View mRootAnimView, Context mContext) {
		super(mRootAnimView, mContext);
		mRocketObject = new RocketObject(mRootAnimView, mContext);
		mLaunchPadObject = new LaunchPadObject(mRootAnimView, mContext);
		mLaunchPadObject.setRocketObject(mRocketObject);
		// 添加到处理队列中
		addAnimObject(mLaunchPadObject);
		addAnimObject(mRocketObject);
	}

	@Override
	public boolean onTouch(MotionEvent event) {
		if (!mCanTouch) {
			return true;
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			if (mLaunchPadObject.isReadyOnLaunch()) {
				gotoAnimSecStage();
			}
		}
		return super.onTouch(event);
	}

	/**
	 * 切换到动画第二阶段
	 */
	private void gotoAnimSecStage() {
		mAnimStage = ANIM_STAGE_LAUNCH;
		// 禁止点击事件
		mCanTouch = false;
		// 不再画发射台
		removeAnimObject(mLaunchPadObject);
		// 通知火箭起飞，设置动画阶段
		mRocketObject.setAnimStage(mAnimStage);
		removeAnimObject(mRocketObject);
		// 加入雾霾
		mFogObject = new FogObject(getRootAnimView(), getContext());
		addAnimObject(mFogObject);
		mSmallFogObject = new SmallFogObject(getRootAnimView(), getContext());
		mSmallFogObject.setFogObject(mFogObject);
		addAnimObject(mSmallFogObject);
		addAnimObject(mRocketObject);	// 调整绘制顺序
	}
}
